﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Runtime.InteropServices;
using System.IO;

namespace WhomaShuriken.Core.Utility
{
    public class Util
    {

        private Util() { }//デフォルトコンストラクタの隠蔽

        //***********************************************
        // htons()
        //***********************************************
        public static ushort htons(ushort i)
        {
            return (ushort)((i << 8) + (i >> 8));
        }
        public static uint htonl(uint i)
        {
            return (uint)((i & 0xff000000) >> 24 | (i & 0x00ff0000) >> 8 | (i & 0x0000ff00) << 8 | (i & 0x000000ff) << 24);
        }
        public static UInt64 htonl(UInt64 i)
        {
            return (UInt64)((i & 0xff00000000000000) >> 56 | (i & 0x00ff000000000000) >> 40 | (i & 0x0000ff0000000000) >> 24 | (i & 0x000000ff00000000) >> 8
                           | (i & 0x00000000ff000000) << 8 | (i & 0x0000000000ff0000) << 24 | (i & 0x000000000000ff00) << 40 | (i & 0x00000000000000ff) << 56);
        }

        //string strの中の文字 before を文字 afterに置き換える
        /// <summary>
        /// 文字列置き換え用メソッド
        /// </summary>
        /// <param name="before">置き換え対象文字列</param>
        /// <param name="after">置き換え後文字列</param>
        /// <param name="str">置き換えターゲット文字列</param>
        /// <returns></returns>
        public static string SwapChar(char before, char after, string str)
        {
            while (true)
            {
                int index = str.IndexOf(before);
                if (index < 0)
                    break;
                //\bが指定された場合、ブランクにする
                if (after == '\b')
                {
                    str = str.Substring(0, index) + str.Substring(index + 1);
                }
                else
                {
                    str = str.Substring(0, index) + after + str.Substring(index + 1);
                }
            }
            return str;
        }
        //string strの中の文字 beforeStr を afterStrに置き換える
        /// <summary>
        /// 文字列置き換え用メソッド
        /// </summary>
        /// <param name="beforeStr">置き換え対象文字列</param>
        /// <param name="afterStr">置き換え後文字列</param>
        /// <param name="str">置き換えターゲット文字列</param>
        /// <returns></returns>
        public static string SwapStr(string beforeStr, string afterStr, string str)
        {
            int offset = 0;//処理済み文字位置
            while (true)
            {
                int index = str.Substring(offset).IndexOf(beforeStr);
                if (index < 0)
                    break;
                index += offset;
                str = str.Substring(0, index) + afterStr + str.Substring(index + beforeStr.Length);
                offset = index + afterStr.Length;
            }
            return str;
        }
        //string strの中の文字 c文字が連続している場合1つにする 
        public static string MargeChar(char c, string str)
        {
            char[] buf = new char[2] { c, c };
            string tmpStr = new string(buf);

            while (true)
            {
                int index = str.IndexOf(tmpStr);
                if (index < 0)
                    break;
                str = str.Substring(0, index) + str.Substring(index + 1);
            }
            return str;
        }
        public static string DateStr(DateTime dt)
        {
            string[] monthName = { "", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

            if (DateTime.Now.Year == dt.Year)
            {
                return string.Format("{0} {1:D2} {2:D2}:{3:D2}",
                                        monthName[dt.Month],
                                        dt.Day,
                                        dt.Hour,
                                        dt.Minute);
            }
            return string.Format("{0} {1:D2} {2:D4}",
                                        monthName[dt.Month],
                                        dt.Day,
                                        dt.Year);
        }

        //指定ファイルの中で最初にtag文字列が出現する位置を返す
        public static int IndexOf(string fileName, string tag)
        {
            int len = 0;//カウンタ
            if (File.Exists(fileName))
            {
                using (StreamReader sr = new StreamReader(fileName, Encoding.GetEncoding("Shift_JIS")))
                {
                    while (true)
                    {
                        string str = sr.ReadLine();
                        if (str == null)
                            break;
                        int index = str.IndexOf(tag);
                        if (0 <= index)
                        {
                            sr.Close();
                            return len + index;
                        }
                        len += str.Length + 2;//１行分の文字数でカウンタをアップする
                    }
                    sr.Close();
                }
            }
            return -1;
        }


        //Thu, 27 Nov 2008 20:45:50 GMT
        public static string UtcTime2Str(DateTime dt)
        {
            string[] monthList = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
            string[] weekList = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

            string str = string.Format("{0}, {1:D2} {2} {3:D4} {4:D2}:{5:D2}:{6:D2} GMT"
                , weekList[(int)dt.DayOfWeek]
                , dt.Day
                , monthList[dt.Month - 1]
                , dt.Year
                , dt.Hour
                , dt.Minute
                , dt.Second);
            return str;

        }
        //Sun, 1 Feb 2009 09:28:20 +0900
        public static string LocalTime2Str(DateTime dt)
        {
            string[] monthList = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
            string[] weekList = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

            TimeSpan span = TimeZone.CurrentTimeZone.GetUtcOffset(dt);
            int hour = span.Hours;
            string zoneStr = string.Format("+{0:D2}00", hour);
            if (hour < 0)
            {
                zoneStr = string.Format("-{0:D2}00", hour * (-1));
            }
            string str = string.Format("{0}, {1:D2} {2} {3:D4} {4:D2}:{5:D2}:{6:D2} {7}"
                , weekList[(int)dt.DayOfWeek]
                , dt.Day
                , monthList[dt.Month - 1]
                , dt.Year
                , dt.Hour
                , dt.Minute
                , dt.Second
                , zoneStr);
            return str;

        }
        public static DateTime Str2Time(string str)
        {
            string[] monthList = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

            try
            {
                string[] tmp = str.Split(' ');
                string[] time = tmp[4].Split(':');

                int year = Convert.ToInt32(tmp[3]);
                int month = 0;
                for (int m = 0; m < monthList.Length; m++)
                {
                    if (monthList[m] == tmp[2])
                    {
                        month = m + 1;
                        goto next;
                    }
                }
                return new DateTime(0);
            next:
                int day = Convert.ToInt32(tmp[1]);
                int hour = Convert.ToInt32(time[0]);
                int minute = Convert.ToInt32(time[1]);
                int second = Convert.ToInt32(time[2]);
                DateTime dt = new DateTime(year, month, day, hour, minute, second);

                return dt;
            }
            catch
            {
                return new DateTime(0);
            }

        }
    }
}
